package zhoufu.sz.sanxiao_check.ui.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.common.DividerItemDecoration;
import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.FragmentRefreshrecyclerBinding;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.presents.RefreshRecyclerViewPresenter;
import zhoufu.sz.sanxiao_check.ui.adapter.FlexibleDBAdapter;
import zhoufu.sz.sanxiao_check.weiget.MultipleStatusView;

/**
 * 项目： SanXiao_Check
 * 时间： 2016/6/27 16:18
 * 人员： hyc
 * 功能： 带有刷新和上拉 加载的Fragment,需继承使用
 */

public abstract class RefreshRecyclerViewFragment extends BaseFragment implements FlexibleAdapter.EndlessScrollListener,
        BGARefreshLayout.BGARefreshLayoutDelegate {

    private FragmentRefreshrecyclerBinding _binding;
    private MultipleStatusView             mEmptyView;
    private RecyclerView                   mRecyclerView;
    private BGARefreshLayout               mRefreshLayout;
    private FlexibleDBAdapter              _adapter;
    @Inject
    RefreshRecyclerViewPresenter _recyclerViewPresenter;

    public BGARefreshLayout getmRefreshLayout() {
        return mRefreshLayout;
    }

    public FlexibleDBAdapter getAdapter() {

        RecyclerView.Adapter adapter = getmRecyclerView().getAdapter();
        if (adapter instanceof FlexibleDBAdapter) {
            return (FlexibleDBAdapter) adapter;
        }
        return null;
    }

    public FragmentRefreshrecyclerBinding getBinding() {
        return _binding;
    }

    public MultipleStatusView getmEmptyView() {
        return mEmptyView;
    }

    public RecyclerView getmRecyclerView() {
        return mRecyclerView;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        _binding = DataBindingUtil.inflate(inflater, R.layout.fragment_refreshrecycler,
                container, false);
        LoginComponet component = getComponent(LoginComponet.class);
        injectThis(component);
        setHasOptionsMenu(true);
        return _binding.getRoot();
    }

    protected abstract void injectThis(LoginComponet component);

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mEmptyView = _binding.mainMultiplestatusview;

        getmEmptyView().setOnRetryClickListener(this);
        mRecyclerView = _binding.recyclerView;
//        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this.getContext(), R.drawable.layout_divider));
        LinearLayoutManager layout = new SmoothScrollLinearLayoutManager(this.getContext(),
                LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layout);

        mRefreshLayout = _binding.refreshLayout;
        mRefreshLayout.setDelegate(this);

        BGANormalRefreshViewHolder viewHolder = new BGANormalRefreshViewHolder(this.getContext(), false);
        mRefreshLayout.setRefreshViewHolder(viewHolder);
        mRefreshLayout.setPullDownRefreshEnable(true);

        _recyclerViewPresenter.onStart();
    }

    protected void switchRefresh(boolean refresh) {
        mRefreshLayout.setPullDownRefreshEnable(refresh);

    }


    public void showErrorView() {
        mEmptyView.showError();
    }
    public void showConnError(){
        mEmptyView.showNoNetwork();
    }
    public void showContent() {
        mEmptyView.showContent();
    }

    public void showEmptyView() {
        mEmptyView.showEmpty();
    }

    public void showLoadView() {
        mEmptyView.showLoading();
    }

    public void setAdapter(FlexibleDBAdapter adapter) {
        if (!(adapter instanceof FlexibleDBAdapter)) {
            throw new ClassFormatError("Adapter 必须继承自 FlexibleDBAdapter");
        }
        _adapter = adapter;
//        _adapter.setAnimationOnReverseScrolling(true);
//        _adapter.setAnimationOnScrolling(true);
//        _adapter.setEndlessScrollListener(this,new ProgressItem());
//        _adapter.setEndlessScrollThreshold(1);//@IntRange(from=1);
        mRecyclerView.setAdapter(_adapter);
    }

    public void endMore() {
        mRefreshLayout.endLoadingMore();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {

    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {

        return false;
    }

    protected void endRefresh() {
        if (mRefreshLayout != null) {
            mRefreshLayout.endRefreshing();
        }
    }

    protected void endLoadMore() {
        if (mRefreshLayout != null) {
            mRefreshLayout.endLoadingMore();
        }
    }


}